﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using ExtAspNet;

public partial class Module_StaffManger_StaffTree : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btncancel.OnClientClick = ActiveWindow.GetHideReference();

        if (!IsPostBack)
        {
            loadTv();
        }
    }

    private void loadTv()
    {
        Ylh.BLL.DepartmentBLL bll = new Ylh.BLL.DepartmentBLL(DbConnectionStr);

        DataTable dt = bll.GetDepartMentInfo(-1);

        ExtAspNet.TreeNode tn;
        foreach (DataRow dr in dt.Rows)
        {
            tn = new ExtAspNet.TreeNode();
            tn.Text = dr["DepartmentName"].ToString();
            tn.NodeID = dr["DepartmentID"].ToString();

            tn.EnableCheckBox = false;
            tn.Leaf = false;
            tn.Expanded = false;
            tn.AutoPostBack = false;
            Tree1.Nodes.Add(tn);
        }



    }
    protected void Tree1_NodeExpand(object sender, TreeExpandEventArgs e)
    {
        Ylh.BLL.StaffBLL blls = new Ylh.BLL.StaffBLL(DbConnectionStr);


        DataTable dt = blls.GetStaffData(e.NodeID);


        ExtAspNet.TreeNode tn;
        foreach (DataRow dr in dt.Rows)
        {
            tn = new ExtAspNet.TreeNode();
            tn.Text = dr["StaffName"].ToString();

            tn.NodeID = dr["StaffID"].ToString();

            tn.ToolTip = dr["Tel"] != null ? dr["Tel"].ToString() : "";

            tn.Leaf = true;
            tn.EnableCheckBox = true;
            tn.Checked = false;
            tn.Checked = false;
            e.Node.Nodes.Add(tn);
        }

        e.Node.Expanded = true;

    }
    protected void btnconfirm_Click(object sender, EventArgs e)
    {

        ExtAspNet.TreeNode[] tns = Tree1.GetCheckedNodes();
        if (tns.Count() == 0)
        {
            Alert.Show("请先选择一个员工");
            return;
        }

        if (tns.Count() > 1)
        {
            Alert.Show("只能选择一个员工");
            return;
        }

        if (Request.QueryString["SetStaff"] != null)
        {
            Ylh.BLL.CustBLL bllc = new Ylh.BLL.CustBLL(DbConnectionStr);
            bllc.UpdateServerInfo(Request.QueryString["custID"].ToString(), tns[0].NodeID);
            //直接设置员工信息,不返回信息

            PageContext.RegisterStartupScript(ActiveWindow.GetHideReference());
        }
        else if (Request.QueryString["DepartmentID"] != null)
        {
            //设置部门负责人
            Ylh.BLL.DepartmentBLL bll = new Ylh.BLL.DepartmentBLL(DbConnectionStr);
            bll.SetDeparmentHead(tns[0].NodeID, Request.QueryString["DepartmentID"].ToString());
            PageContext.RegisterStartupScript(ActiveWindow.GetHideReference());
        }
        else if (Request.QueryString["CustID"]!=null)
        {
            if (tns.Count() == 0)
            {
                Alert.ShowInParent("只能选择一个员工");
                return;
            }

            if (tns.Count() > 1)
            {
                Alert.ShowInParent("只能选择一个员工");
                return;
            }
            //返回员工信息,电话,ID,名称   
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(tns[0].NodeID, tns[0].ToolTip, tns[0].Text, Request.QueryString["CustID"].ToString()) + ActiveWindow.GetHidePostBackReference());
        }
        else
        {
            if (tns.Count() == 0)
            {
                Alert.ShowInParent("只能选择一个员工");
                return;
            }

            if (tns.Count() > 1)
            {
                Alert.ShowInParent("只能选择一个员工");
                return;
            }
            //返回员工信息,电话,ID,名称   
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(tns[0].NodeID, tns[0].ToolTip, tns[0].Text) + ActiveWindow.GetHideReference());
        }
    }

    protected void Tree1_NodeCheck(object sender, ExtAspNet.TreeCheckEventArgs e)
    {
        if (e.Checked)
        {
            Tree1.CheckAllNodes(e.Node.Nodes);
        }
        else
        {
            Tree1.UncheckAllNodes(e.Node.Nodes);
        }

    }
}